package configurationReader;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Piotr
 */
public class ConfigReader {

    public static void loadConfigFile() {
        try {
            Document doc = readConfigFile();
            readAdministratorSettings(doc.getElementsByTagName("administrator"));
            readSpacesSettings(doc.getElementsByTagName("spaces"));
            readXamppSettings(doc.getElementsByTagName("xampp"));
        } catch (IOException | ParserConfigurationException | DOMException | SAXException e) {
            Logger.getLogger(ConfigReader.class.getName()).log(Level.SEVERE, "[CarPark] Cannot load config file", e);
        }
    }

    private static Document readConfigFile() throws SAXException, ParserConfigurationException, IOException {
        File fXmlFile = new File(".\\src\\main\\resources\\config.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();
        return doc;
    }

    private static void readAdministratorSettings(NodeList nList) throws DOMException {
        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                Config.INSTANCE.setAdminPassword(eElement.getElementsByTagName("password").item(0).getTextContent());
            }
        }
    }
    
    private static void readSpacesSettings(NodeList nList) throws DOMException {
        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                Config.INSTANCE.setPassesConfigurationLocation(eElement.getElementsByTagName("location").item(0).getTextContent());
            }
        }
    }
    
    private static void readXamppSettings(NodeList nList) throws DOMException {
        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                Config.INSTANCE.setXampLocation(eElement.getElementsByTagName("location").item(0).getTextContent());
                Config.INSTANCE.setXamppDbName(eElement.getElementsByTagName("dbName").item(0).getTextContent());
                Config.INSTANCE.setXamppDbUser(eElement.getElementsByTagName("dbUser").item(0).getTextContent());
                Config.INSTANCE.setXamppDbPassword(eElement.getElementsByTagName("dbPass").item(0).getTextContent());
            }
        }
    }


}
